import csv
from datetime import datetime,timedelta

pdict = {}
bdict = {}
bldict = {}

bfile = csv.reader(open('blocks.csv'), delimiter=',')
for block in bfile:
    bdict[block[0]] = block[1]

pfile = csv.reader(open('persons.csv'), delimiter=',')
for person in pfile:
    pdict[person[0]] = person[1]
    bldict[person[0]] = bdict[person[2]]

mfile = csv.reader(open('messages.csv'), delimiter=',')

output = open('06.06.10.html', 'w')
output.write('<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><style>.date {color:#ACACAC;} .nick{color:blue;} .mute{color:grey;}</style></head><body><table>')
prepeared_messages = [message for message in mfile]
prepeared_messages.sort(lambda a,b: cmp(a[3],b[3]))
for message in prepeared_messages:
    #if message[2] == 'False':
    date = datetime.strptime(message[3][:len(message[3]) - 7], "%Y-%m-%d %H:%M:%S")
    date += timedelta(hours=3)
    cls = 'mute' if message[2] == 'True' or bldict[message[4]] == 'True' else '' 
    s = "<tr><td class='date'>%s</td><td class='nick'>%s</td><td class='message %s'>%s</td></tr>\n" % (date.strftime("%H:%M:%S"), pdict[message[4]],cls, message[1])
    output.write(s)
output.write('</tabel></body></html>')
